package com.fyzk.activity.lottery.kaijiang;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.prize.InquiryInfo;
import com.fyzk.adapter.PrizeLotteryAdapter;
import com.fyzk.bean.LotteryLayoutItem;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.HomeTopTitleView;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

import static com.alibaba.fastjson.JSON.parseArray;

public class PrizeListActivity extends BaseActivity {
    private HomeTopTitleView topTitle;
    private UltimateRecyclerView mLVi_prizenums;
    private LinearLayoutManager linearLayoutManager;

    private PrizeLotteryAdapter mAdpt_PrizeLottery;
    private Animation anim;

    String lotterys ;
    List<InquiryInfo> inquiryInfos_show = new ArrayList<InquiryInfo>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_prizelist);

        init();
        initData();
        net();
    }

    List<LotteryLayoutItem> lotteryLayoutItemList;

    protected void request() {
        JSONObject params = new JSONObject();
        httpPostJson(CommandUtil.HISTORY_LOTTERY, Commands.FINDLASTWINCODE, params, false);
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        switch (requestCode){
            case CommandUtil.HISTORY_LOTTERY:
                try {
                    if (resultBean.getErrorcode().equals("0")) {
                        JSONObject obj = new JSONObject(resultBean.getData());
                        Iterator<String> sts = obj.keys();
                        ArrayList<InquiryInfo> inquiryInfos = new ArrayList<InquiryInfo>();

                        while (sts.hasNext()) {
                            inquiryInfos.add(JSON.parseObject(obj.getString(sts.next()), InquiryInfo
                                    .class));
                        }
                        initLotterys();
                        refreshLotteryList(inquiryInfos);
                        setList();
                    } else {
                        AppToast.getToast().show(R.string.net_error);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case CommandUtil.LOTTERYTYPELAYOUT:
                lotteryLayoutItemList = parseArray(resultBean.getData(), LotteryLayoutItem.class);

                StringBuffer currentLotetry = new StringBuffer();
                for (LotteryLayoutItem info : lotteryLayoutItemList) {
                    currentLotetry.append(info.getLotteryType());
                    currentLotetry.append(",");
                }
                if(currentLotetry.length()>1){
                    currentLotetry.deleteCharAt(currentLotetry.length()-1);
                }
                MySharedPreferences.getInstance().setCurrentLotterys(currentLotetry.toString());
                initLotterys();
                request();
                break;
        }
    }

    private void refreshLotteryList(ArrayList<InquiryInfo> netLotteryList) {
        for (InquiryInfo info : netLotteryList) {
            if (inquiryInfos_show.contains(info)) {
                int index = inquiryInfos_show.indexOf(info);
                InquiryInfo local = inquiryInfos_show.get(index);
                local.wincode = info.wincode;
                local.phase = info.phase;
                local.drawTime = info.drawTime;
            }
        }
    }

    private void net() {
        topTitle.getRightImgCom().startAnimation(anim);
        lotterys =   MySharedPreferences.getInstance().getCurrentLotterys();
        LogUtil.v("lotterys:>>"+lotterys);
        if(!TextUtil.isEmpty(lotterys)){
            request();
        }else{
            requestLotteryTypeLayout();
        }
    }

    List<InquiryInfo> inquiryInfos = new ArrayList<InquiryInfo>();

    private void initData() {
        InquiryInfo info_ssq = new InquiryInfo();
        info_ssq.lotteryType = LotteryIDUtils.SSQ_LOTTERY_ID_INT;
        info_ssq.lotteryName = getString(R.string.ssq);
        inquiryInfos.add(info_ssq);
        InquiryInfo info_dlt = new InquiryInfo();
        info_dlt.lotteryType = LotteryIDUtils.DLT_LOTTERY_ID_INT;
        info_dlt.lotteryName = getString(R.string.dlt);
        inquiryInfos.add(info_dlt);
        InquiryInfo info_fc3d = new InquiryInfo();
        info_fc3d.lotteryType = LotteryIDUtils.FC3D_LOTTERY_ID_INT;
        info_fc3d.lotteryName = getString(R.string.fc3d);
        inquiryInfos.add(info_fc3d);
        InquiryInfo info_jc = new InquiryInfo();
        info_jc.lotteryType = LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT;
        info_jc.lotteryName = getString(R.string.jcz);
        inquiryInfos.add(info_jc);
        InquiryInfo info_jclq = new InquiryInfo();
        info_jclq.lotteryType = LotteryIDUtils.JLSF_LOTTERY_ID_INT;
        info_jclq.lotteryName = getString(R.string.jclq);
        inquiryInfos.add(info_jclq);
        InquiryInfo info_bd = new InquiryInfo();
        info_bd.lotteryType = LotteryIDUtils.DCSPF_LOTTERY_ID_INT;
        info_bd.lotteryName = getString(R.string.danchang);
        inquiryInfos.add(info_bd);
        InquiryInfo info_pl3 = new InquiryInfo();
        info_pl3.lotteryType = LotteryIDUtils.PL3_LOTTERY_ID_INT;
        info_pl3.lotteryName = getString(R.string.pl3);
        inquiryInfos.add(info_pl3);
        InquiryInfo info_pl5 = new InquiryInfo();
        info_pl5.lotteryType = LotteryIDUtils.PL5_LOTTERY_ID_INT;
        info_pl5.lotteryName = getString(R.string.pl5);
        inquiryInfos.add(info_pl5);
        InquiryInfo info_qxc = new InquiryInfo();
        info_qxc.lotteryType = LotteryIDUtils.QXC_LOTTERY_ID_INT;
        info_qxc.lotteryName = getString(R.string.qxc);
        inquiryInfos.add(info_qxc);
        InquiryInfo info_qlc = new InquiryInfo();
        info_qlc.lotteryType = LotteryIDUtils.QLC_LOTTERY_ID_INT;
        info_qlc.lotteryName = getString(R.string.qlc);
        inquiryInfos.add(info_qlc);
        InquiryInfo info_klpk = new InquiryInfo();
        info_klpk.lotteryType = LotteryIDUtils.KLPK_LOTTERY_ID_INT;
        info_klpk.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.KLPK_LOTTERY_ID_INT);
        inquiryInfos.add(info_klpk);
//        InquiryInfo info_nxK3 = new InquiryInfo();
//        info_nxK3.lotteryType = LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT;
//        info_nxK3.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT);
//        inquiryInfos.add(info_nxK3);
        InquiryInfo info_ahk3 = new InquiryInfo();
        info_ahk3.lotteryType = LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT;
        info_ahk3.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT);
        inquiryInfos.add(info_ahk3);
        InquiryInfo info_jsK3 = new InquiryInfo();
        info_jsK3.lotteryType = LotteryIDUtils.JSK3_LOTTERY_ID_INT;
        info_jsK3.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.JSK3_LOTTERY_ID_INT);
        inquiryInfos.add(info_jsK3);
        InquiryInfo info_jlK3 = new InquiryInfo();
        info_jlK3.lotteryType = LotteryIDUtils.JLK3_LOTTERY_ID_INT;
        info_jlK3.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.JLK3_LOTTERY_ID_INT);
        inquiryInfos.add(info_jlK3);
        InquiryInfo info_gxK3 = new InquiryInfo();
        info_gxK3.lotteryType = LotteryIDUtils.GXK3_LOTTERY_ID_INT;
        info_gxK3.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.GXK3_LOTTERY_ID_INT);
        inquiryInfos.add(info_gxK3);
        InquiryInfo info_klsf = new InquiryInfo();
        info_klsf.lotteryType = LotteryIDUtils.KLSF_LOTTERY_ID_INT;
        info_klsf.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.KLSF_LOTTERY_ID_INT);
        inquiryInfos.add(info_klsf);
        InquiryInfo info_gdklsf = new InquiryInfo();
        info_gdklsf.lotteryType = LotteryIDUtils.GDKLSF_LOTTERY_ID_INT;
        info_gdklsf.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.GDKLSF_LOTTERY_ID_INT);
        inquiryInfos.add(info_gdklsf);
        InquiryInfo info_11x5 = new InquiryInfo();
        info_11x5.lotteryType = LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT;
        info_11x5.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils
                .SD11XUAN5_LOTTERY_ID_INT);
        inquiryInfos.add(info_11x5);
        InquiryInfo info_gd11x5 = new InquiryInfo();
        info_gd11x5.lotteryType = LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT;
        info_gd11x5.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils
                .GD11XUAN5_LOTTERY_ID_INT);
        inquiryInfos.add(info_gd11x5);
        InquiryInfo info_xj11x5 = new InquiryInfo();
        info_xj11x5.lotteryType = LotteryIDUtils.XJ11XUAN5_LOTTERY_ID_INT;
        info_xj11x5.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils
                .XJ11XUAN5_LOTTERY_ID_INT);
        inquiryInfos.add(info_xj11x5);
        InquiryInfo info_ssc = new InquiryInfo();
        info_ssc.lotteryType = LotteryIDUtils.CQSSC_LOTTERY_ID_INT;
        info_ssc.lotteryName = AppLotteryInfos.getLotteryName(LotteryIDUtils.CQSSC_LOTTERY_ID_INT);
        inquiryInfos.add(info_ssc);
        InquiryInfo info_zc = new InquiryInfo();
        info_zc.lotteryType = LotteryIDUtils.ZCSFC_LOTTERY_ID_INT;
        info_zc.lotteryName = getString(R.string.zc);
        inquiryInfos.add(info_zc);
        InquiryInfo info_zcrx9 = new InquiryInfo();
        info_zcrx9.lotteryType = LotteryIDUtils.ZCRXJ_LOTTERY_ID_INT;
        info_zcrx9.lotteryName = getString(R.string.zc_rx9);
        inquiryInfos.add(info_zcrx9);
        InquiryInfo info_zcbqc = new InquiryInfo();
        info_zcbqc.lotteryType = LotteryIDUtils.ZCBQC_LOTTERY_ID_INT;
        info_zcbqc.lotteryName = getString(R.string.zc_bqc);
        inquiryInfos.add(info_zcbqc);
        InquiryInfo info_zcjqc = new InquiryInfo();
        info_zcjqc.lotteryType = LotteryIDUtils.ZCJQC_LOTTERY_ID_INT;
        info_zcjqc.lotteryName = getString(R.string.zc_jqc);
        inquiryInfos.add(info_zcjqc);
    }

    private void initLotterys(){
        if(TextUtils.isEmpty(lotterys)){
            lotterys =   MySharedPreferences.getInstance().getCurrentLotterys();
        }
        inquiryInfos_show.clear();
        LogUtil.v("lotterys:>>>>"+lotterys);
        String[] lotteryArr = lotterys.split(",");
        for (InquiryInfo info : inquiryInfos) {
            for(String lottery : lotteryArr){
                int lotteryId = Integer.parseInt(lottery);
                if(info.lotteryType == lotteryId){
                    inquiryInfos_show.add(info);
                }
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void requestLotteryTypeLayout() {
        JSONObject json = new JSONObject();
        httpPostJson(CommandUtil.LOTTERYTYPELAYOUT, Commands.LOTTERYTYPELAYOUT, json, true);
    }

    protected void init() {
        anim = AnimationUtils.loadAnimation(this, R.anim.rotate);
        topTitle = (HomeTopTitleView)findViewById(R.id.prize_topTitle);
        topTitle.setCenterText("开奖公告");
        topTitle.setOnTitleClickListener(this);
        topTitle.getRightImgCom().setAnimation(anim);
        mLVi_prizenums = (UltimateRecyclerView)findViewById(R.id.dj_lVi_peizelist);

        linearLayoutManager = new LinearLayoutManager(this);
        mLVi_prizenums.setLayoutManager(linearLayoutManager);

        mLVi_prizenums.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        net();
                    }
                }, 1000);
            }
        });

    }

    private void setList() {
        LogUtil.v("size>>>>>"+inquiryInfos_show.size());
        if (mAdpt_PrizeLottery == null) {
            LogUtil.v("null>>>>>");
            mAdpt_PrizeLottery = new PrizeLotteryAdapter(this, inquiryInfos_show);
            mLVi_prizenums.setAdapter(mAdpt_PrizeLottery);
        } else {
            linearLayoutManager.scrollToPosition(0);
            mLVi_prizenums.setAdapter(mAdpt_PrizeLottery);
            mAdpt_PrizeLottery.notifyDataSetChanged();
        }
    }

    public Context getContext() {
        return this;
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onCenterClick() {
    }

    @Override
    public void onRightClick() {
        net();
    }


    @Override
    public void onRight2Click() {

    }

}
